package com.javacv.plus.extensions.audio.steps;

import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.core.processor.ProcessorStep;
import com.javacv.plus.extensions.audio.AudioProcessor;
import com.javacv.plus.extensions.audio.AudioResult;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;

/**
 * 加载音频步骤
 */
@Slf4j
public class LoadAudioStep implements ProcessorStep<AudioProcessor, AudioResult> {
    @Override
    public AudioResult execute(AudioProcessor processor, ProcessorContext context) throws Exception {
        String inputPath = processor.getConfig().getInputPath();
        log.info("Loading audio file: {}", inputPath);

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputPath);
        grabber.setFormat("auto");
        grabber.start();

        // 设置音频信息到上下文
        context.setAttribute("grabber", grabber);
        context.setAttribute("sampleRate", grabber.getSampleRate());
        context.setAttribute("channels", grabber.getAudioChannels());
        context.setAttribute("bitRate", grabber.getAudioBitrate());
        context.setAttribute("format", grabber.getFormat());

        log.info("Audio loaded successfully: {}Hz, {} channels, {}bps",
                grabber.getSampleRate(),
                grabber.getAudioChannels(),
                grabber.getAudioBitrate());
        return null; // 中间步骤不返回结果
    }
    @Override
    public String getStepName() {
        return "LoadAudio";
    }
} 